package net.openadventure;

import java.util.ArrayList;

/**
 * Accepts a script and parses it into a series of Director commands.
 * @author paul
 *
 */
public class ScriptParser {

	private ScriptParser() {
		
	}
	
	public static String[] parseScript(ArrayList<String> lineList) {
		// TODO temp parsing code
		
		// Convert lines to string array
		String[] lines = new String[lineList.size()];
		lineList.toArray(lines);
		
		for(int i = 0; i < lines.length; i++) {
			
			String [] parts = lines[i].split("\\s+");
			
			if(parts[0].equalsIgnoreCase("print")) {
				StringBuilder sb = new StringBuilder();
				
				sb.append(parts[0]);
				sb.append("|");
				
				for(int p = 1; p < parts.length; p++) {
					sb.append(parts[p]);
					sb.append(" ");
				}
				
				sb.deleteCharAt(sb.length() - 1);
				
				lines[i] = sb.toString();
			} else if(parts[0].equalsIgnoreCase("addSprite")) {
				StringBuilder sb = new StringBuilder();
				
				for(String part : parts) {
					sb.append(part);
					sb.append("|");
				}
				
				sb.deleteCharAt(sb.length() - 1);
				
				lines[i] = sb.toString();
			} else if(parts[0].equalsIgnoreCase("removeAllSprites")) {
				lines[i] = parts[0];
			} else if(parts[0].equalsIgnoreCase("goto")) {
				StringBuilder sb = new StringBuilder();
				
				sb.append(parts[0]);
				sb.append("|");
				sb.append(parts[1]);

				lines[i] = sb.toString();
			} else if(parts[0].equalsIgnoreCase("loopSprite")) {
				StringBuilder sb = new StringBuilder();
				
				for(String part : parts) {
					sb.append(part);
					sb.append("|");
				}
				
				sb.deleteCharAt(sb.length() - 1);
				
				lines[i] = sb.toString();
			}
		}
		return lines;
	}
}